package cn.vc.content.service.article;

import cn.vc.content.entity.article.ArticleLike;
import cn.vc.content.entity.video.VideoLike;
import cn.vc.content.mapper.article.ArticleLikeMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class ArticleLikeService {
    @Autowired
    private ArticleLikeMapper articleLikeMapper;



    /**
     * 点赞或者取消点赞
     * @param id 文章id
     * @param userId 用户id
     * @return true为点赞。false为取赞
     */
    public boolean like(Long id, Long userId) {
        // 查询是否点赞
        LambdaQueryWrapper<ArticleLike> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ArticleLike::getArticleId, id).eq(ArticleLike::getUserId, userId);
        ArticleLike articleLike = articleLikeMapper.selectOne(lqw);
        // 为空，没有点赞
        if (articleLike==null) {
            articleLike = new ArticleLike();
            articleLike.setArticleId(id);
            articleLike.setUserId(userId);
            articleLikeMapper.insert(articleLike);
            return true;
        }
        // 已经点赞，删除
        else {
            articleLikeMapper.deleteById(articleLike.getId());
            return false;
        }

//        try {
//
//            return true;
//        } catch (Exception e) {
//            log.debug("已经点过赞了："+userId+" -> "+id);
//            this.noLike(id, userId);
//            return false;
//        }
    }


    public int noLike(Long id, Long userId) {
        LambdaQueryWrapper<ArticleLike> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ArticleLike::getArticleId, id).eq(ArticleLike::getUserId, userId);
        ArticleLike articleLike = articleLikeMapper.selectOne(lqw);
        if (articleLike == null) return -1;
        articleLikeMapper.deleteById(articleLike.getId());
        return 1;
    }
}
